package lancoo.com.net.retrofitrxjava.interceptor;

import android.text.TextUtils;
import android.util.Log;


import java.io.IOException;
import java.nio.charset.Charset;

import lancoo.com.net.retrofitrxjava.network.Network;
import okhttp3.Interceptor;
import okhttp3.MediaType;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okio.Buffer;

/**
 * Created by IVAN on 2018/11/12.
 */

public class RequestEncryptInterceptor implements Interceptor {


    @Override
    public Response intercept(Chain chain) throws IOException {
//        String url = URLDecoder.decode(chain.request().url().toString(),"UTF-8");
        String url =chain.request().url().toString();
        Log.e("url",url);
        Request request = chain.request();
        if(!url.contains("Decrypt")){
            return chain.proceed(request);
        }

        String paramsStr="";
        if(TextUtils.equals("GET",request.method())){//get
            int index = url.indexOf("?");
            if(index>0){
                paramsStr=url.substring(index+1);
            }
        }else {
            RequestBody body = request.body();
            if (body != null) {
                Buffer buffer = new Buffer();
                body.writeTo(buffer);
                Charset charset = Charset.forName("UTF-8");
                MediaType contentType = body.contentType();
                if (contentType != null) {
                    charset = contentType.charset(charset);
                }
                paramsStr = buffer.readString(charset);
            }
        }

        try {
//            Log.e("aaa", "加密前的paramsStr:" + paramsStr);
//            paramsStr = EncryptUtil.EncryptCode(CurrentUser.UserID, paramsStr);
            Log.e("aaa", "加密后的paramsStr:" + paramsStr);
        } catch (Exception e) {
            Log.e("network error", e.toString());
        }
        String salt = (Network.serverTime + System.currentTimeMillis() - Network.localTime) + "";
        String AppKey = "123";
        String toData = paramsStr;
//        String sign = EncryptUtil.MD5(AppKey + salt + toData);
        Log.e("salt", salt);
        Log.e("appKey", AppKey);
//        Log.e("sign", sign);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"),
                paramsStr);
        Request.Builder builder = request.newBuilder();
        builder.header("platform", AppKey)//加密因子
                .header("timestamp", salt);//时间戳
//                .header("sign", sign);//校验码
        if (request.body() != null) {
            Request build = builder
                    .post(requestBody)
                    .build();
            return chain.proceed(build);
        } else {
            int index=url.indexOf("?");
            if (index>0) {
                url = url.substring(0,index);
            }

            Request build = builder
                    .url(url + "?" + paramsStr)
                    .build();
            return chain.proceed(build);
        }
    }
}
